# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Timo Teräs <timo.teras@iki.fi>
pkgname=grub
pkgver=2.06
pkgrel=5
pkgdesc="Bootloader with support for Linux, Multiboot and more"
url="https://www.gnu.org/software/grub/"
arch="all !s390x"
license="GPL-3.0-or-later"
depends="initramfs-generator"
triggers="grub.trigger=/boot"
makedepends="bison flex linux-headers xz-dev lvm2-dev fuse-dev
	automake autoconf libtool python3 freetype-dev unifont gettext-dev"
install="$pkgname.post-upgrade"
# strip handled by grub Makefiles, abuild strip breaks xen pv-grub
options="!check !strip"  # Cannot test boot loader.
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-mkfont
	$pkgname-mount
	$pkgname-bash-completion:bashcomp:noarch
	"

# currently grub only builds on x86*, aarch64 and ppc* systems
case "$CARCH" in
	x86)		flavors="efi bios";;
	x86_64)		flavors="efi bios xenhost xenhost_pvh";;
	aarch64|arm*)	flavors="efi";;
	mips*)		flavors="qemu_mips";;
	riscv*)		flavors="efi";;
	ppc*)		flavors="ieee1275"; makedepends="$makedepends powerpc-utils" ;;
	s390x)		flavors="emu" ;;
esac
for f in $flavors; do
	[ x"$f" = x"xenhost_pvh" ] && continue # xenhost_pvh shipped in the xenhost subpackage
	subpackages="$subpackages $pkgname-$f"
done

source="https://ftp.gnu.org/gnu/grub/grub-$pkgver.tar.xz
	fix-gcc-no-pie-specs.patch
	grub-xen-host_grub.cfg
	alpine-mkconfig.patch
	lang-C.UTF-8.patch
	0001-loader-drop-argv-argument-in-grub_initrd_load.patch
	0002-efi-add-definition-of-LoadFile2-protocol.patch
	0003-efi-implemented-LoadFile2-initrd-loading-protocol-fo.patch
	0004-linux-ignore-FDT-unless-we-need-to-modify-it.patch
	0005-loader-Move-arm64-linux-loader-to-common-code.patch
	0006-RISC-V-Update-image-header.patch
	0007-RISC-V-Use-common-linux-loader.patch
	0008-fix-build-error-in-binutils-2.36.patch
	0009-fix-kernel-version-detection.patch
	0010-riscv-binutils-2.38.patch
	0011-mkconfig-ignore-apk-new-files.patch
	0012-remove-udevadm-usage.patch
	"

prepare() {
	default_prepare
	PYTHON=python3 sh ./autogen.sh
}

_build_flavor() {
	local flavor="$1"
	shift
	local _configure="$@"

	msg "Building grub for platform $flavor"
	mkdir -p "$srcdir"/build-$flavor
	cd "$srcdir"/build-$flavor
	$builddir/configure \
		--build=$CBUILD \
		--host=$CHOST \
		--target=$CTARGET \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--disable-nls \
		--disable-werror \
		$_configure
	make
}

build() {
	local f
	for f in $flavors; do
		case "$f" in
		bios)		_build_flavor $f --with-platform=pc;;
		efi)		_build_flavor $f --with-platform=efi --disable-efiemu;;
		xenhost)	_build_flavor $f --with-platform=xen;;
		xenhost_pvh)	_build_flavor $f --with-platform=xen_pvh --target=i386;;
		*)		_build_flavor $f --with-platform=$f;;

		esac
	done
}

_install_xen() {
	grub_dir=$(mktemp -d)
	cfg=$(mktemp)
	grub_memdisk=$(mktemp)

	mkdir -p $grub_dir/boot/grub
	echo 'normal (memdisk)/grub.cfg' > $cfg
	sed -e "s/@@PVBOOT_ARCH@@/$CARCH/g" \
		$srcdir/grub-xen-host_grub.cfg \
			> $grub_dir/grub.cfg
	tar -cf - -C $grub_dir grub.cfg > $grub_memdisk

	./grub-mkimage \
		-O $CARCH-xen \
		-c $cfg \
		-d ./grub-core ./grub-core/*.mod \
		-m $grub_memdisk \
		-o $pkgdir/grub-$CARCH-xen.bin

	rm -r "$grub_dir"
	rm "$cfg" "$grub_memdisk"
}

_install_xenpvh() {
	grub_dir=$(mktemp -d)
	cfg=$(mktemp)
	grub_memdisk=$(mktemp)

	mkdir -p $grub_dir/boot/grub
	echo 'normal (memdisk)/grub.cfg' > $cfg
	sed -e "s/@@PVBOOT_ARCH@@/$CARCH/g" \
		$srcdir/grub-xen-host_grub.cfg \
			> $grub_dir/grub.cfg
	tar -cf - -C $grub_dir grub.cfg > $grub_memdisk

	echo "About to run grub-mkimage in: $PWD"
	# Note: only i386-xen_pvh supported
	./grub-mkimage \
		-O i386-xen_pvh \
		-c $cfg \
		-d ./grub-core ./grub-core/*.mod \
		-m $grub_memdisk \
		-o $pkgdir/grub-i386-xen_pvh.bin

	rm -r "$grub_dir"
	rm "$cfg" "$grub_memdisk"
}

_install_flavor() {
	local flavor="$1"
	cd "$srcdir"/build-$flavor
	case $flavor in
	xenhost)	_install_xen;;
	xenhost_pvh)	_install_xenpvh;;
	*)	 	make DESTDIR="$pkgdir" install-strip;;
	esac
}

package() {
	# install BIOS & EFI version into the same directory
	# and overwrite similar files.
	for f in $flavors; do
		_install_flavor $f
	done

	rm -f "$pkgdir"/usr/lib/charset.alias
	# remove grub-install warning of missing directory
	mkdir -p "$pkgdir"/usr/share/locale

	mkdir -p "$pkgdir"/etc/default/
	cat >"$pkgdir"/etc/default/grub <<-EOF
	GRUB_DISTRIBUTOR="Alpine"
	GRUB_TIMEOUT=2
	GRUB_DISABLE_SUBMENU=y
	GRUB_DISABLE_RECOVERY=true
	EOF

	mkdir -p "$pkgdir"/usr/share/bash-completion
	mv "$pkgdir"/etc/bash_completion.d "$pkgdir"/usr/share/bash-completion/completions
}

bios() {
	pkgdesc="$pkgdesc (BIOS version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-pc $subpkgdir/usr/lib/grub/
}

efi() {
	pkgdesc="$pkgdesc (EFI version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-efi $subpkgdir/usr/lib/grub/
}

xenhost() {
	pkgdesc="$pkgdesc (XEN host version)"
	mkdir -p $subpkgdir/usr/lib/grub-xen
	mv $pkgdir/*-xen.bin $subpkgdir/usr/lib/grub-xen/

	# pick up result from xenhost_pvh build as well
	mv $pkgdir/*-xen_pvh.bin $subpkgdir/usr/lib/grub-xen/
}

qemu_mips() {
	pkgdesc="$pkgdesc (QEMU MIPS version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-qemu_mips $subpkgdir/usr/lib/grub/
}

ieee1275() {
	pkgdesc="$pkgdesc (IEEE1275 version)"
	depends="$pkgname powerpc-utils"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-ieee1275 $subpkgdir/usr/lib/grub/
}

emu() {
	pkgdesc="$pkgdesc (EMU version)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/lib/grub
	mv $pkgdir/usr/lib/grub/*-emu $subpkgdir/usr/lib/grub/
}

mkfont() {
	pkgdesc="$pkgdesc (mkfont utility)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/bin
	mv $pkgdir/usr/bin/grub-mkfont $subpkgdir/usr/bin/
}

mount() {
	pkgdesc="$pkgdesc (mount utility)"
	depends="$pkgname"
	mkdir -p $subpkgdir/usr/bin
	mv $pkgdir/usr/bin/grub-mount $subpkgdir/usr/bin/
}

bashcomp() {
	depends=""
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	amove usr/share/bash-completion/completions
}

sha512sums="
4f11c648f3078567e53fc0c74d5026fdc6da4be27d188975e79d9a4df817ade0fe5ad2ddd694238a07edc45adfa02943d83c57767dd51548102b375e529e8efe  grub-2.06.tar.xz
f2a7d9ab6c445f4e402e790db56378cecd6631b5c367451aa6ce5c01cd95b95c83c3dd24d6d4b857f8f42601eba82c855607513eb6ce5b2af6bd6c71f046e288  fix-gcc-no-pie-specs.patch
4e7394e0fff6772c89683039ccf81099ebbfe4f498e6df408977a1488fd59389b6e19afdbf0860ec271e2b2aea0df7216243dcc8235d1ca3af0e7f4d0a9d60a4  grub-xen-host_grub.cfg
84d9694af384336c0846faad968a63b154e2bd957671fa7c0163c36e20fd086a8d5b67ddf5eb983a11a69f65dac52668d0f51537e7cfe9a1ea9bca7aba91f20b  alpine-mkconfig.patch
f23702741f2a37c83d0cbf16a000a6229dfd433cd583f3254d523ce35b3668c1db2d8a704a75013e74655ec4f9da5db6b630aa92e3ca5cc47df44cc9e41d1806  lang-C.UTF-8.patch
c0ea8ef9a3d4e511fae97a4b90dc6f407c5b0a81c922924992f3eccd49fad0886f2e06ff21731cc59d42a6ac28272eb859a7ee9f14642ae1ad705f2cd1da1956  0001-loader-drop-argv-argument-in-grub_initrd_load.patch
708c3e8ca60638b7961aac64fe1eecb21ef9db844b4a21c2d36303c7fcd71173ba13dc0df3c8ae09b4a0a6a4a5b7fb3c41736a9bf6d1ef367c73dcf5bdf17668  0002-efi-add-definition-of-LoadFile2-protocol.patch
aa2fbabeab2e8f517128f0d81d3c76eba18b24518b616d4574ffaaec7d4765e02b5b118caf04da1e225342a66700f2774c6a04c1ebfd32747499eb3cbc9b72ea  0003-efi-implemented-LoadFile2-initrd-loading-protocol-fo.patch
6616373b6c94f48dd8ee0636973451a7cf492ea8dfc854b9c250bcc291d5c3a9efae51ad9d37b7298dc8e6f22041109b4ad8e82cf75c9514b5dfaa78a02d9ce1  0004-linux-ignore-FDT-unless-we-need-to-modify-it.patch
74b2bf8e9da604c5ca60ed13e389e48ceb4eeea1669b8ac720cdc452a4450676fe4bb961a63d31603bcdc9fe15ab6a8cb74b9ff97b6d7d85f1aaba24b1e9e244  0005-loader-Move-arm64-linux-loader-to-common-code.patch
4901c964278ab8a46901b905efef4562aa4942d6c37be052db9536d4779ecea6a0cf68fcc97fe6ef2b39c54cd598d4f8351cf3e27c3dacff888549bdc35deee3  0006-RISC-V-Update-image-header.patch
721c9d7aefd9d9fdd4fe92c229122c3311e75266a9419a12f5f77536099070a232b6c4ff3c33ab5a2cb6f7f917ae0d6510b09045d2f035f87c65b4dd7f09d3ee  0007-RISC-V-Use-common-linux-loader.patch
8c35663ef72683bd23c18e75134a0ddb1bb9a7c053ed87691e2ac5c9765fdf802761a6ad54f8bdc7999679d9cc5ebea9c01f1c0f615ac90505f7a398129d157b  0008-fix-build-error-in-binutils-2.36.patch
d4432a36286f0ad7d3527d34e20daf9fbef24d6037171582ae3ccdd51668ffe422b87d984f107a9b4dcf019b89ac76dd322d64e42bbfa64bb6789d3d897fc1f2  0009-fix-kernel-version-detection.patch
4b8b5eeeb45dd9fddc9af9b6e694644470672b35af33cd11f1578f35503b08662c51f27c10bffeb96a51de0a879c741c0d22600410147eb20505efe1a3b50b54  0010-riscv-binutils-2.38.patch
229fe710de9fb106e08457a5a19dd3cc1746389ff1cdd95721b092913c51550fc8ad05141a23fc396a9e8dbbcec909920eda5f3eed339af9938774feb9e6c7bc  0011-mkconfig-ignore-apk-new-files.patch
c6011ece89e55d92e351bdab3eae180d75fcde642865934800641d42969febebd3a29a68941a10ef42a0bdeae0069432f2bb31e42fa651263cdedc1b964d5b33  0012-remove-udevadm-usage.patch
"
